package com.kawaiior.asoulworld.events;


import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import java.util.Random;

public class ASEventHelper {

    private static int AVA_ARMOR_INT = 0;


    static float getCarolDamageMultiple(float health)
    {
        if (health>19)
        {
            return 1.1f;
        }
        else if (health>18)
        {
            return 1.2f;
        }
        else if (health>17)
        {
            return 1.3f;
        }
        else if (health>16)
        {
            return 1.4f;
        }
        else if (health>15)
        {
            return 1.5f;
        }
        else if (health>14)
        {
            return 1.6f;
        }
        else if (health>13)
        {
            return 1.7f;
        }
        else if (health>12)
        {
            return 1.8f;
        }
        else if (health>11)
        {
            return 1.9f;
        }
        else if (health>10)
        {
            return 2f;
        }
        else if (health>9)
        {
            return 2.5f;
        }
        else if (health>8)
        {
            return 3f;
        }
        else if (health>7)
        {
            return 3.5f;
        }
        else if (health>6)
        {
            return 4f;
        }
        else if (health>5)
        {
            return 4.5f;
        }
        else if (health>4)
        {
            return 5f;
        }
        else if (health>3)
        {
            return 7f;
        }
        else if (health>=2)
        {
            return 10f;
        }
        else if (health<2)
        {
            return 20f;
        }
        return 1.0f;
    }

    //已弃用
    static boolean playerAvaAttack(float amount)
    {
        if (amount<6)
        {
            AVA_ARMOR_INT += 1;
            if (AVA_ARMOR_INT >= 9)
            {
                AVA_ARMOR_INT = 0;
                return true;
            }
            return false;
        }
        else
        {
            AVA_ARMOR_INT = 0;
            return true;
        }
    }

    //传送玩家至64格以内
    static void teleportSure(double x, double y, double z, World world, EntityPlayer player){
        Random rand = new Random();
        double d0 = x + (rand.nextDouble() - 0.5D) * 64.0D;
        double d1 = y + (double)(rand.nextInt(64) - 32);
        double d2 = z + (rand.nextDouble() - 0.5D) * 64.0D;

        BlockPos blockPos0 = new BlockPos(d0,d1-1.0D,d2);
        BlockPos blockPos1 = new BlockPos(d0,d1,z);
        BlockPos blockPos2 = new BlockPos(d0,d1+1.0D,d2);

        world.setBlockState(blockPos0, Blocks.OBSIDIAN.getDefaultState());
        world.setBlockToAir(blockPos1);
        world.setBlockToAir(blockPos2);
    }

    //随机传送玩家至64格以内
    public static boolean teleportRandomly(double x, double y, double z, World world, EntityPlayer player){
        Random rand = new Random();
        double d0 = x + (rand.nextDouble() - 0.5D) * 64.0D;
        double d1 = y + (double)(rand.nextInt(64) - 32);
        double d2 = z + (rand.nextDouble() - 0.5D) * 64.0D;
        return teleportTo(d0,d1,d2,world,player);
    }

    //随机传送玩家至128格以内
    public static boolean teleportRandomly128(double x, double y, double z, World world, EntityPlayer player){
        Random rand = new Random();
        double d0 = x + (rand.nextDouble() - 0.5D) * 128.0D;
        double d1 = y + (double)(rand.nextInt(128) - 64);
        double d2 = z + (rand.nextDouble() - 0.5D) * 128.0D;
        return teleportTo(d0,d1,d2,world,player);
    }

    public static boolean teleportRandomly(double x, double y, double z, World world, EntityLiving living){
        Random rand = new Random();
        double d0 = x + (rand.nextDouble() - 0.5D) * 64.0D;
        double d1 = y + (double)(rand.nextInt(64) - 32);
        double d2 = z + (rand.nextDouble() - 0.5D) * 64.0D;
        return teleportEntityTo(d0,d1,d2,world,living);
    }

    /**
     * 优化下面的传送方法
     * 未充分测试
     */
    public static void teleportToEntity(EntityPlayer player, Entity entityLivingBase){
        double x = entityLivingBase.posX;
        double y = entityLivingBase.posY;
        double z = entityLivingBase.posZ;
        double px = player.posX;
        double py = player.posY;
        double pz = player.posZ;
        double distance = (pz-z)/(px-x);
        if (px > x){
            if (distance < -(2/0.5)){
                player.setPositionAndUpdate(x,y,z-3);
            }
            else if (distance < -(2/1.5)){
                player.setPositionAndUpdate(x+1,y,z-2.5);
            }
            else if (distance < -(1.5/2)){
                player.setPositionAndUpdate(x+2,y,z-2);
            }
            else if (distance < -(0.5/2)){
                player.setPositionAndUpdate(x+2.5,y,z-1);
            }
            else if (distance < (0.5/2)){
                player.setPositionAndUpdate(x+3,y,z);
            }
            else if (distance < (1.5/2)){
                player.setPositionAndUpdate(x+2.5,y,z+1);
            }
            else if (distance < (2/1.5)){
                player.setPositionAndUpdate(x+2,y,z+2);
            }
            else if (distance < 2/0.5){
                player.setPositionAndUpdate(x+1,y,z+2.5);
            }
            else {
                player.setPositionAndUpdate(x,y,z+3);
            }
        }
        else if (px < x){
            if (distance < -(2/0.5)){
                player.setPositionAndUpdate(x,y,z+3);
            }
            else if (distance < -(2/1.5)){
                player.setPositionAndUpdate(x-1,y,z+2.5);
            }
            else if (distance < -(1.5/2)){
                player.setPositionAndUpdate(x-2,y,z+2);
            }
            else if (distance < -(0.5/2)){
                player.setPositionAndUpdate(x-2.5,y,z+1);
            }
            else if (distance < (0.5/2)){
                player.setPositionAndUpdate(x-3,y,z);
            }
            else if (distance < (1.5/2)){
                player.setPositionAndUpdate(x-2.5,y,z-1);
            }
            else if (distance < (2/1.5)){
                player.setPositionAndUpdate(x-2,y,z-2);
            }
            else if (distance < 2/0.5){
                player.setPositionAndUpdate(x-1,y,z-2.5);
            }
            else {
                player.setPositionAndUpdate(x,y,z-3);
            }
        }
        else {
            if (pz >z){
                player.setPositionAndUpdate(x,y,z+3);
            }
            else if (pz < z){
                player.setPositionAndUpdate(x,y,z-3);
            }
        }
    }

    /**
     * 传送玩家至目标地点相距一格的一个位置
     * 这个位置会尽量还原玩家和目标地点之间的角度
     * 这个算法简单粗暴，但是效率不够高，容易出问题，不过能跑
     */
    public static void teleportTo(double x, double y, double z, double px, double py, double pz, EntityPlayer player){
        double distance = ((pz-z)*(pz-z))/((px-x)*(px-x));
        if (px > x){
            if (pz > z){
                if (distance<=(0.5D/3D)*(0.5D/3D)){
                    player.setPositionAndUpdate(x+3,y,z);
                }
                else if (distance<=(1.5D/3D)*(1.5D/3D)){
                    player.setPositionAndUpdate(x+3,y,z+1);
                }
                else if (distance<=(2.5D/3D)*(2.5D/3D)){
                    player.setPositionAndUpdate(x+3,y,z+2);
                }
                else if (distance<=(3D/2.5D)*(3D/2.5D)){
                    player.setPositionAndUpdate(x+3,y,z+3);
                }
                else if (distance<=(3D/1.5D)*(3D/1.5D)){
                    player.setPositionAndUpdate(x+2,y,z+3);
                }
                else if (distance<=(3D/0.5D)*(3D/0.5D)){
                    player.setPositionAndUpdate(x+1,y,z+3);
                }
                else
                {
                    player.setPositionAndUpdate(x,y,z+3);
                }
            }
            else if (pz < z){
                if (distance<=(0.5D/3D)*(0.5D/3D)){
                    player.setPositionAndUpdate(x+3,y,z);
                }
                else if (distance<=(1.5D/3D)*(1.5D/3D)){
                    player.setPositionAndUpdate(x+3,y,z-1);
                }
                else if (distance<=(2.5D/3D)*(2.5D/3D)){
                    player.setPositionAndUpdate(x+3,y,z-2);
                }
                else if (distance<=(3D/2.5D)*(3D/2.5D)){
                    player.setPositionAndUpdate(x+3,y,z-3);
                }
                else if (distance<=(3D/1.5D)*(3D/1.5D)){
                    player.setPositionAndUpdate(x+2,y,z-3);
                }
                else if (distance<=(3D/0.5D)*(3D/0.5D)){
                    player.setPositionAndUpdate(x+1,y,z-3);
                }
                else
                {
                    player.setPositionAndUpdate(x,y,z+3);
                }
            }
            else
            {
                player.setPositionAndUpdate(x+3,y,z);
            }
        }
        else if (px < x){
            if (pz > z){
                if (distance<=(0.5D/3D)*(0.5D/3D)){
                    player.setPositionAndUpdate(x-3,y,z);
                }
                else if (distance<=(1.5D/3D)*(1.5D/3D)){
                    player.setPositionAndUpdate(x-3,y,z+1);
                }
                else if (distance<=(2.5D/3D)*(2.5D/3D)){
                    player.setPositionAndUpdate(x-3,y,z+2);
                }
                else if (distance<=(3D/2.5D)*(3D/2.5D)){
                    player.setPositionAndUpdate(x-3,y,z+3);
                }
                else if (distance<=(3D/1.5D)*(3D/1.5D)){
                    player.setPositionAndUpdate(x-2,y,z+3);
                }
                else if (distance<=(3D/0.5D)*(3D/0.5D)){
                    player.setPositionAndUpdate(x-1,y,z+3);
                }
                else
                {
                    player.setPositionAndUpdate(x,y,z+3);
                }
            }
            else if (pz < z){
                if (distance<=(0.5D/3D)*(0.5D/3D)){
                    player.setPositionAndUpdate(x-3,y,z);
                }
                else if (distance<=(1.5D/3D)*(1.5D/3D)){
                    player.setPositionAndUpdate(x-3,y,z-1);
                }
                else if (distance<=(2.5D/3D)*(2.5D/3D)){
                    player.setPositionAndUpdate(x-3,y,z-2);
                }
                else if (distance<=(3D/2.5D)*(3D/2.5D)){
                    player.setPositionAndUpdate(x-3,y,z-3);
                }
                else if (distance<=(3D/1.5D)*(3D/1.5D)){
                    player.setPositionAndUpdate(x-2,y,z-3);
                }
                else if (distance<=(3D/0.5D)*(3D/0.5D)){
                    player.setPositionAndUpdate(x-1,y,z-3);
                }
                else
                {
                    player.setPositionAndUpdate(x,y,z-3);
                }
            }
            else
            {
                player.setPositionAndUpdate(x-3,y,z);
            }
        }
        else
        {
            if (pz > z){
                player.setPositionAndUpdate(x,y,z+3);
            }
            else if (pz < z){
                player.setPositionAndUpdate(x,y,z-3);
            }
        }

    }

    /**
     *传送玩家
     * 如果传送点有两格及以上的空气方块，传送玩家并返回true
     * 反之，不传送玩家并返回false
     */
    private static boolean teleportTo(double x, double y, double z, World world, EntityPlayer player){
        BlockPos blockPos0 = new BlockPos(x,y-1.0D,z);
        BlockPos blockPos1 = new BlockPos(x,y,z);
        BlockPos blockPos2 = new BlockPos(x,y+1.0D,z);
        if (!world.isAirBlock(blockPos0) && world.isAirBlock(blockPos1) && world.isAirBlock(blockPos2)){
            player.setPositionAndUpdate(x,y,z);
            world.setBlockState(blockPos0, Blocks.OBSIDIAN.getDefaultState());
            return true;
        }
        return false;
    }

    private static boolean teleportEntityTo(double x, double y, double z, World world, EntityLiving living){
        BlockPos blockPos0 = new BlockPos(x,y-1.0D,z);
        BlockPos blockPos1 = new BlockPos(x,y,z);
        BlockPos blockPos2 = new BlockPos(x,y+1.0D,z);
        if (!world.isAirBlock(blockPos0) && world.isAirBlock(blockPos1) && world.isAirBlock(blockPos2)){
            living.setPositionAndUpdate(x,y,z);
            world.setBlockState(blockPos0, Blocks.OBSIDIAN.getDefaultState());
            return true;
        }
        return false;
    }

}
